﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using LayeredArchitectureSample.DataTransferObjects;
using LayeredArchitectureSample.DomainModel;
using LayeredArchitectureSample.Infrastructure;
using LayeredArchitectureSample.ServiceContracts;

namespace LayeredArchitectureSample.Services
{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single)]
    [ExceptionMarshallingBehavior]
    public class RemoteRouteRepository : IRemoteRouteRepository
    {
        private readonly IRouteRepository _repository;

        public RemoteRouteRepository(IRouteRepository repository)
        {
            if (repository == null) throw new ArgumentNullException("repository");

            _repository = repository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <exception cref="FaultException{CannotRemoveEntityFault}"></exception>
        /// <exception cref="FaultException{InvalidArgumentFault}"></exception>
        /// <exception cref="FaultException{EntityNotFoundFault}"></exception>
        public void RemoveRoute(long id)
        {
            _repository.RemoveRoute(id);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <exception cref="FaultException{InvalidArgumentFault}"></exception>
        /// <exception cref="FaultException{CannotAddEntityFault}"></exception>
        public RouteDto AddNewRoute(IEnumerable<LocationDto> locations)
        {
            List<Location> locationsListed = locations.Select(l => new Location(l.Id, l.Name)).ToList();

            var newRoute = _repository.AddNewRoute(locationsListed);

            return new RouteDto(newRoute);
        }

        public IEnumerable<RouteDto> GetAll()
        {
            return new List<RouteDto>(
                _repository
                .GetAll()
                .Select(x => new RouteDto(x)));
        }
    }
}